package com.pc.tas.learning.trace.index;

import com.pc.cloud.core.SearchType;
import com.pc.cloud.services.DocumentBuilder;
import com.pc.tas.learning.trace.Trace;
import com.pc.tas.util.IPUtil;
import com.pc.tas.util.SearchEngineUtils;
import com.pc.tas.util.Utils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

public class TraceDocumentBuilder implements DocumentBuilder {
	
	public static Map<String,Integer> sessionMap = null;
	private Collection<Trace> traces;
	
	public String getSearchType() {
		return SearchType.TRACE;
	}
	
	public TraceDocumentBuilder(Collection<Trace> traces) {
		this.traces = traces;
	}
	
    public List<Document> getDocs() throws Exception {
        List<Document> docs = new ArrayList<Document>();
        try{
        	for(Trace trace : this.traces) {
        		Document doc = new Document();        	
            	doc.add(new Field(TraceSearchFields.Keyword.ID, trace.getId(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                doc.add(new Field(TraceSearchFields.Keyword.PAGE_ID, String.valueOf(trace.getPageId()), Field.Store.YES, Field.Index.NOT_ANALYZED));

            	long ip = IPUtil.ip2Long(trace.getIP());
            	doc.add(new Field(TraceSearchFields.Keyword.IP, String.valueOf(ip), Field.Store.YES, Field.Index.NOT_ANALYZED));
            	
            	if(trace.getClientId() != null && !"".equals(trace.getClientId().trim())){
            		//System.out.println("uv:" + trace.getClientId());
            		doc.add(new Field(TraceSearchFields.Keyword.UV,trace.getClientId(),Field.Store.NO, Field.Index.NOT_ANALYZED));
            	}
            	
            	String time = TraceSearchFields.dateFormat.format(trace.getCreatedate());
            	doc.add(new Field(TraceSearchFields.Keyword.TIME,time,Field.Store.NO, Field.Index.NOT_ANALYZED));
            	
            	String day = TraceSearchFields.dayFormat.format(trace.getCreatedate());
            	System.out.println(day);
            	doc.add(new Field(TraceSearchFields.Keyword.DAY,day,Field.Store.NO, Field.Index.NOT_ANALYZED));
            	
            	String month = TraceSearchFields.monthFormat.format(trace.getCreatedate());
            	doc.add(new Field(TraceSearchFields.Keyword.MONTH,month,Field.Store.NO, Field.Index.NOT_ANALYZED));
            	
            	try{
        			Calendar c = Calendar.getInstance();
        			c.setTime(trace.getCreatedate());
        			int h = c.get(Calendar.HOUR_OF_DAY);
        			String hour = String.valueOf(h);
        			if(h < 10){
        				hour = "0" + hour;
        			}
        			doc.add(new Field(TraceSearchFields.Keyword.HOUR,hour,Field.Store.NO, Field.Index.NOT_ANALYZED));
        		}catch(Exception e){
        			e.printStackTrace();
        		}
            	this.addCategory(doc, String.valueOf(trace.getPageId()));
            	//String areas = IPUtil.getArea(trace.getIP());
            	//this.addArea(doc, areas);
            	
            	String referrer = trace.getReferer();
            	if(referrer != null && !"".equals(referrer)) {
            		doc.add(new Field(TraceSearchFields.Keyword.REFERRER, referrer, Field.Store.NO, Field.Index.NOT_ANALYZED));
            		
            		String se = SearchEngineUtils.getInstance().getSearchEngine(referrer);
            		
            		if(se != null && !"".equals(se)) {
            			doc.add(new Field(TraceSearchFields.Keyword.SEARCH_ENGINE, se, Field.Store.NO, Field.Index.NOT_ANALYZED));
            			
            			String seKeyword = SearchEngineUtils.getInstance().getKeyword(referrer);
            			if(seKeyword != null && !"".equals(seKeyword)) {
            				doc.add(new Field(TraceSearchFields.Keyword.SE_KEYWORD, seKeyword, Field.Store.NO, Field.Index.NOT_ANALYZED));
            			}
            		}            		
            	}
                if(trace.getUrl() != null) {
                	doc.add(new Field(TraceSearchFields.Keyword.URL, trace.getUrl(), Field.Store.NO, Field.Index.NOT_ANALYZED));
                }
                docs.add(doc);
        	}        	
        }catch(Exception e){
            e.printStackTrace();
        }
        return docs;
    }
	private void addArea(Document doc, String areas) throws Exception {
		if(areas != null && areas.length() > 0){
			doc.add(new Field(TraceSearchFields.Keyword.AREA, areas, Field.Store.NO, Field.Index.NOT_ANALYZED));
		}
		String[] regions = areas.split("/");
		int i = 0;
		//StringBuilder temp = new StringBuilder();
		for(String region : regions) {
			if(region==null || "".equals(region.trim())) {
				continue;
			}
			//temp.append(region);
			//temp.append("/");
			doc.add(new Field(TraceSearchFields.Keyword.AREA, region, Field.Store.NO, Field.Index.NOT_ANALYZED));
			doc.add(new Field(TraceSearchFields.Keyword.AREA + i, region, Field.Store.YES, Field.Index.NOT_ANALYZED));
			i++;
		}
	}

    private void addCategory(Document doc, String categoryPath) throws Exception {
        if (categoryPath != null && categoryPath.length() > 0) {
            doc.add(new Field(TraceSearchFields.Keyword.PAGE_CATEGORY, categoryPath, Field.Store.NO, Field.Index.NOT_ANALYZED));
        }
        String[] categorys = categoryPath.split("/");
        int i = 0;
        StringBuilder temp = new StringBuilder();
        for (String c : categorys) {
            if (c == null || "".equals(c.trim())) {
                continue;
            }
            i++;
            temp.append(c);
            temp.append("/");
            doc.add(new Field(TraceSearchFields.Keyword.CATEGORY, temp.toString()
                    , Field.Store.NO, Field.Index.NOT_ANALYZED));

            doc.add(new Field(TraceSearchFields.Keyword.CATEGORY + i, c
                    , Field.Store.NO, Field.Index.NOT_ANALYZED));
        }
    }

	public Collection<String> getKeys() throws Exception {
	    List<String> keys = new ArrayList<String>();
	    for(Trace trace : this.traces) {
	    	keys.add(trace.getId());
	    }        
        return keys;
	}	
	
	public static void main(String[] args){
		String dd = "2012-02-08 00:27:30";
		try{
			Date d = TraceSearchFields.dateFormat.parse(dd);
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			System.out.println(c.get(Calendar.HOUR_OF_DAY));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
